import scrapy
from ..items import ImgspiderItem

class ImgSpiderSpider(scrapy.Spider):
    name = 'img_spider'
    # allowed_domains = ['baidu.com']
    base_url = 'https://fabiaoqing.com/biaoqing/lists/page/{}.html'
    page = 1
    start_urls = ['https://fabiaoqing.com/biaoqing/lists/page/1.html']

    def parse(self, response):
        imgs = response.xpath('//img[@class="ui image lazy"]')
        print(len(imgs))
        for img in imgs:
            content = img.xpath('@data-original').get()
            title = img.xpath('@title').get()
            yield scrapy.Request(content, callback=self.save_img, cb_kwargs={'a': title})

        if self.page < 5:
            self.page += 1
            yield scrapy.Request(url=self.base_url.format(self.page), callback=self.parse)
            print(f"第{self.page}已经爬完")
        # print(response.text)

    def save_img(self, response, **kwargs):
        item = ImgspiderItem()
        item['content'] = response.body
        item['title'] = kwargs['a']
        yield item

'https://img.soutula.com/bmiddle/ceeb653ely8hh363k4sywj20hs0hm3zm.jpg'
'https://img.soutula.com/large/ceeb653ely8hh363k4sywj20hs0hm3zm.jpg'